package com.fulei.logic;

import com.bjfl.saleManager.util.HexByteDumper;
import com.bjfl.saleManager.util.LogUtil;
import com.dwin.navy.serialportapi.SerailPortOpt;

public class GetSecurityNumber
{
  private static SerailPortOpt serialPort = null;
  public static String comNo = "";
  public static String baudRate = "";
  
  public static boolean OpenPort()
  {
    try
    {
      serialPort = new SerailPortOpt();
      
      serialPort.mDevNum = Integer.parseInt(comNo);
      serialPort.mSpeed = Integer.parseInt(baudRate);
      serialPort.mDataBits = 8;
      serialPort.mStopBits = 1;
      serialPort.mParity = 110;
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("41" + ex.getMessage());
    }
    return true;
  }
  
  public static void closeSerialPort()
  {
    try
    {
      if (serialPort.mFd != null) {
        serialPort.closeDev(serialPort.mFd);
      }
      serialPort.mFd = null;
      serialPort = null;
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("42" + ex.getMessage());
    }
  }
  
  public static String ReadSecurityNumber()
  {
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] SendData = { 27, 49 };
      if (null != serialPort.mFd) {
        serialPort.writeBytes(SendData);
      }
      LogUtil.writePrizeLog("SecurityNumberwrite:" + LogUtil.bytesToHexString(SendData, SendData.length));
      



      byte[] ConfirmData = new byte[512];
      int ConfirmDataLength = serialPort.readBytes(ConfirmData, 400, 40);
      if (ConfirmDataLength < 0) {
        return null;
      }
      LogUtil.writePrizeLog("SecurityNumberread:" + LogUtil.bytesToHexString(ConfirmData, ConfirmDataLength));
      if (ConfirmDataLength > 17)
      {
        byte[] buffer = new byte[ConfirmDataLength];
        serialPort.getInputStream().read(buffer);
        System.arraycopy(ConfirmData, 0, buffer, 0, ConfirmDataLength);
        String code = getStringFromData(buffer, 0, buffer.length);
        if ((buffer[(buffer.length - 2)] == 13) && (buffer[(buffer.length - 1)] == 10))
        {
          int start = code.indexOf("0");
          return code.substring(start, code.length() - 2);
        }
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("ReadSecurityNumber：" + ex.getMessage());
      return null;
    }
    return null;
  }
  
  public static String ReadSportLoterySecurityNumber()
  {
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] SendData = { 27, 49 };
      if (null != serialPort.mFd) {
        serialPort.writeBytes(SendData);
      }
      LogUtil.writePrizeLog("write:" + LogUtil.bytesToHexString(SendData, SendData.length));
      

      byte[] ConfirmData = new byte[512];
      int ConfirmDataLength = serialPort.readBytes(ConfirmData, 400, 40);
      if (ConfirmDataLength < 0) {
        return null;
      }
      LogUtil.writeKeyLog("read:" + LogUtil.bytesToHexString(ConfirmData, ConfirmDataLength));
      if ((41 > ConfirmDataLength) && (28 < ConfirmDataLength))
      {
        byte[] buffer = new byte[ConfirmDataLength];
        serialPort.getInputStream().read(buffer);
        System.arraycopy(ConfirmData, 0, buffer, 0, ConfirmDataLength);
        String code = getStringFromData(buffer, 0, buffer.length);
        if ((buffer[(buffer.length - 2)] == 13) && (buffer[(buffer.length - 1)] == 10)) {
          return code;
        }
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog(ex.getMessage());
    }
    return null;
  }
  
  public static String ReadWelfareLotterySecurityNumber()
  {
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] SendData = { 27, 49 };
      if (null != serialPort.mFd) {
        serialPort.writeBytes(SendData);
      }
      LogUtil.writePrizeLog("write:" + LogUtil.bytesToHexString(SendData, SendData.length));
      

      byte[] ConfirmData = new byte[512];
      int ConfirmDataLength = serialPort.readBytes(ConfirmData, 300, 40);
      if (ConfirmDataLength < 0) {
        return null;
      }
      LogUtil.writeKeyLog("read:" + LogUtil.bytesToHexString(ConfirmData, ConfirmDataLength));
      if (ConfirmDataLength > 20)
      {
        byte[] buffer = new byte[ConfirmDataLength];
        serialPort.getInputStream().read(buffer);
        System.arraycopy(ConfirmData, 0, buffer, 0, ConfirmDataLength);
        String code = getStringFromData(buffer, 0, buffer.length);
        if ((buffer[(buffer.length - 2)] == 13) && (buffer[(buffer.length - 1)] == 10))
        {
          code = code.substring(1, code.length() - 2);
          System.out.println("扫码结果:" + code);
          return code;
        }
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("扫码结果异常:" + ex.getMessage());
    }
    return null;
  }
  
  public static String getStringFromData(byte[] data, int start, int len)
  {
    byte[] bts = new byte[len];
    System.arraycopy(data, start, bts, 0, len);
    return HexByteDumper.toStringHex(HexByteDumper.getHexDump(bts));
  }
}
